package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.RoleDao;
import com.itheima.domain.system.Role;
import com.itheima.factory.MapperFactory;
import com.itheima.service.system.RoleService;
import com.itheima.utils.ArrayToListUtil;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;


public class RoleServiceImpl implements RoleService {
    @Override
    public PageInfo findAll(Integer page, Integer size) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);
        PageHelper.startPage(page,size);
        List<Role> all = mapper.findAll();
        PageInfo<Role> rolePageInfo = new PageInfo<>(all);
        TransactionUtil.close(sqlSession);
        return rolePageInfo;
    }

    @Override
    public List<Role> findAll() {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);
        List<Role> all = mapper.findAll();
        TransactionUtil.close(sqlSession);
        return all;
    }

    @Override
    public void updateRoleModule(String roleId, String...moduleIds) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);

//            System.out.println(Arrays.toString(moduleIds));
            mapper.deleteRoleModule(roleId);
            if(!"[]".equals(Arrays.toString(moduleIds))){

                mapper.saveRoleModule(roleId, moduleIds);
            }

            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public List<Role> findByUserId(String id) {

        SqlSession sqlSession = MapperFactory.getSqlSession();
        RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);
        List<Role> all = mapper.findByUserId(id);
        TransactionUtil.close(sqlSession);
        return all;

    }

    @Override
    public Role findById(String id) {

        SqlSession sqlSession = MapperFactory.getSqlSession();
        RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);
        Role role= mapper.findById(id);
        TransactionUtil.close(sqlSession);
        return role;
    }

    @Override
    public void delete(String[] id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);
            mapper.delete(id);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public void save(Role role) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);
            role.setId(UUID.randomUUID().toString());
            role.setState((long) 1);
            mapper.save(role);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public void update(Role role) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            RoleDao mapper = MapperFactory.getMapper(sqlSession, RoleDao.class);
            mapper.update(role);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

}
